<?php
/**
 *银行
*/

namespace app\admin\controller\Record;
use PhpOffice\PhpWord\TemplateProcessor;
use app\admin\controller\Admin;
use think\facade\App;
use think\facade\Db;
use xhadmin\service\admin\record\BankService;
use xhadmin\db\Bank as BankDb;
use think\facade\Cache;

class Bank extends Admin {


	/*介绍人管理*/
	function index(){
        //p(session('admin'));exit;
        if (!$this->request->isAjax()){
			return $this->display('record/bank/index');
		}else{
			$limit  = $this->request->post('limit', 0, 'intval');
			$offset = $this->request->post('offset', 0, 'intval');
            $type = $this->request->post('type', 0, 'intval');
			$page   = floor($offset / $limit) +1 ;
            $bank_id = $this->request->post('bank_id', 0, 'intval');
			$aba_code= $this->request->param('aba_code', '', 'strip_tags,trim');
			$bank_name = $this->request->param('bank_name', '', 'strip_tags,trim');
            if(!empty($bank_name)){
                $where['bank_name'] = ['like','%'.$bank_name.'%'];
            }
            if(!empty($aba_code)){
                $where['aba_code'] = ['like','%'.$aba_code.'%'];
            }
            if(!empty($bank_id)){
                $where['id'] = ['=',$bank_id];
            }
            if($type==1) {

            }
            $limit = $type==1?($page-1) * $limit.','.$limit:999999999;
			//$limit = ($page-1) * $limit.','.$limit;
            //p($where);exit;
			try{
				$res = BankService::pageList(formatWhere($where),$limit,'*',$orderby = '');
				$list = $res['list'];
			}catch(\Exception $e){
				exit($e->getMessage());
			}

			$data['rows']  = $list;
			$data['total'] = $res['count'];
			return json($data);
		}
	}

	/*添加*/
	function add(){
		if (!$this->request->isPost()){
			return $this->display('record/bank/info');
		}else{
            //$data = $this->request->post();
            //p($data);exit;
            $postField = 'aba_code,bank_name,branch_bank,bank_no';
			$data = $this->request->only(explode(',',$postField),'post');
			//p($data);exit;

            $is_idcard = BankDb::countList(['name'=>$data['name']]);
            if($is_idcard) {
                return json(['status'=>'01','msg'=>'该介绍人已经存在']);
            }
			try {
                BankService::add($data);
			} catch (\Exception $e) {
				$this->error($e->getMessage());
			}
			return json(['status'=>'00','msg'=>'添加成功']);
		}
	}

	/*修改*/
	function update(){
		if (!$this->request->isPost()){
			$id = $this->request->get('id','','intval');
			if(!$id) $this->error('参数错误');
			$this->view->assign('info',checkData(BankDb::getInfo($id)));
			return $this->display('record/bank/info');
		}else{
            $postField = 'id,aba_code,bank_name,branch_bank,bank_no';
			$data = $this->request->only(explode(',',$postField),'post');
			try {
                BankService::update($data);
			} catch (\Exception $e) {
				$this->error($e->getMessage());
			}
			return json(['status'=>'00','msg'=>'修改成功']);
		}
	}


	/*删除*/
	function delete(){
		$idx =  $this->request->post('ids', '', 'strip_tags');
		if(!$idx) $this->error('参数错误');
		try{
			$where['id'] = explode(',',$idx);
            BankService::delete($where);
		}catch(\Exception $e){
			$this->error($e->getMessage());
		}
		return json(['status'=>'00','msg'=>'操作成功']);
	}



    public function checkName(){
        $name=$this->request->param('name', '', 'strip_tags,trim');
        $ret = BankDb::countList(['name'=>$name]);
        if($ret) {
            return json(['status'=>'01','msg'=>'该客户已经存在']);
        }
        return json(['status'=>'00','msg'=>'']);
    }



}

